package example.controller;

import example.common.Result;
import example.entity.Template;
import example.service.ITemplateService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/api/template")
public class TemplateController {

    @Resource
    private ITemplateService templateService;

    @GetMapping("/list")
    public Result getTemplateList() {
        return templateService.getTemplateList();
    }

    @PostMapping("/add")
    public Result addTemplate(@RequestBody Template template) {
        return templateService.addTemplate(template);
    }

    @PostMapping("/upload")
    public Result uploadTemplate(@RequestParam MultipartFile file, @RequestParam Long templateId) {
        return templateService.uploadTemplate(file, templateId);
    }

    @GetMapping("/download/{id}")
    public void downTemplate(@PathVariable("id") Long id, HttpServletResponse response) {
        templateService.downTemplate(id, response);
    }

    @GetMapping("/name/{id}")
    public Result getFileName(@PathVariable("id") Long id) {
        return templateService.getFileName(id);
    }

    @GetMapping("/del/{id}")
    public Result delTemplate(@PathVariable("id") Long id) {
        return templateService.delTemplate(id);
    }

    @GetMapping("/names")
    public Result getTemplateNames() {
        return templateService.getTemplateNames();
    }

    @PostMapping("/addT")
    public Result addSelectionTemplate(@RequestParam Long selectionId, @RequestParam List<String> templateNames) {
        return templateService.addSelectionTemplate(selectionId, templateNames);
    }

    @GetMapping("get/{id}")
    public Result getL(@PathVariable("id") Long selectionId) {
        return templateService.getL(selectionId);
    }

    @GetMapping("/dis/del/{id}")
    public Result delTemp(@PathVariable("id") Long id) {
        return templateService.delTemp(id);
    }
}
